<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 节点模型
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class NodeModel extends CommonModel{

	/**
     * 返回对象的定义
     */
    static public function init__(){

        return array(

            'table_name' => 'node',

            'props' => array(

                // 主键应该是只读，确保领域对象的“不变量”
                'node_id' => array( 'readonly' => true ),

                // 添加对象间的关联
				'GroupModel' => array( Db::BELONGS_TO => 'GroupModel', 'target_key' => 'group_id' ),
            ),

            'attr_protected' => 'node_id',

            'check' => array(

                'node_name'=>array(

                    array( 'require',G::L( '应用名不能为空' ) ),
    	            array( 'nodeName',G::L( '应用名已经存在' ),'condition'=>'must','extend'=>'callback' ),
                ),

                'node_title'=>array(

                    array( 'require', G::L( '显示名不能为空' ) ),
                ),

    	        'node_parentid'=>array(

    	            array( 'nodeParentId',G::L( '节点不能为自己' ),'condition'=>'must','extend'=>'callback' ),
                ),

                'node_sort'=>array(

    	             array( 'number',G::L( '序号只能是数字' ),'condition'=>'notempty','extend'=>'regex' ),
    	         )

            ),

        );

    }


	/**
     * 开启一个查询，查找符合条件的对象或对象集合
     */
    static function F( ){

        $arrArgs = func_get_args();
        return ModelMeta::instance( __CLASS__ )->findByArgs( $arrArgs );
    }


    /**
     * 返回当前 Model 类的元数据对象
     */
    static function M( ){

        return ModelMeta::instance(__CLASS__);
    }


    /**
     * 节点父级不能为自己
     */
   public function nodeParentId( ){

    	$nNodeId = Safe::getGpc( 'id' );
    	$nNodeParentid = Safe::getGpc( 'node_parentid' );

    	if( ( $nNodeId == $nNodeParentid )
    	       and !empty( $nNodeId )
    	       and !empty( $nNodeParentid ) ){

    		return false;
    	}

    	return true;
    }


    /**
     * 节点名字唯一性验证
     */
    public function nodeName(){

    	$nId = Safe::getGpc( 'id','P' );
    	$sNodeName = Safe::getGpc( 'node_name','P' );

    	$sNodeInfo='';

    	if( $nId ){

    		$arrNode = self::F( 'node_id =?',$nId )->asArray()->getOne( );
    		$sNodeInfo = trim( $arrNode['node_name'] );
    	}

    	if( $sNodeName != $sNodeInfo ){

            $arrResult = self::F()->getBynode_name( $sNodeName )->toArray();

    	    if( !empty( $arrResult['node_id'] ) ) {

        	   return false;
            }

            else{

			   return true;
		    }

    	}

    	return true;
    }

}
